const ajax = require('../../../tool/ajax')
const login = require('../../login/index');
const signaturePreview = require('../../component/signaturePreview')
const caseDetail = require('../../component/caseDetail')
const courtTime = require('../../component/courtTime')
const searchForArbitrators = require('../../component/searchForArbitrators')
let pupConfig, page, config, tribunal, caseInfo;

/**
 * 主任指定仲裁员到确认开庭时间
 */
const start = async (myPupConfig, myCaseInfo) => {
    pupConfig = myPupConfig;
    page = pupConfig.page;
    config = pupConfig.config.common;
    tribunal = pupConfig.config.tribunal;
    caseInfo = myCaseInfo;

    await appointArbitrator();
    await sendNotice();
    await saveCaseInfo();

    return caseInfo;
}

/**
 * 主任指定仲裁员
 */
const appointArbitrator = async () => {
    await login(pupConfig, 'zhuren');
    await pupConfig.page.goto(pupConfig.config.common.host + 'caseDetail?caseId=' + caseInfo.caseId);
    await ajax.waitForResponse(page, 'getAllCaseInfo.do');
    if (await page.$('button.ant-btn[type=button]')) {
        await caseDetail(pupConfig, caseInfo.caseId, 0);
        await appointSingle();
        await appointDiscussion();
    }
    else {
        return
    }

}

/**
 * 主任指定独任制仲裁员
 */
const appointSingle = async () => {
    //判断是否是独任制
    if (tribunal.secretary.courtyardStyle !== 0) {
        return;
    }

    await appointSingleSelect();
    await appointSingleNext();
}

/**
 * 主任指定独任制仲裁员：选择仲裁员
 */
const appointSingleSelect = async () => {
    /**
     * 选择独任仲裁员
     */
    //TODO 1.先判断方式：多个交集,没有交集与1个交集；2.根据交集判断UI与接口；3.获取仲裁员下标
    let res = await ajax.waitForResponse(page, 'getSelectedArbitrator.do');
    if (res.data.isIntersction === '0') {
        await searchForArbitrators(pupConfig, tribunal.zhuren.chiefId)//独任没有交集
    }else if (res.data.isIntersction === '1') {
        await searchForArbitrators(pupConfig, tribunal.zhuren.chiefId,1) //多个交集       
    }
    await screen('主任指定仲裁员待确认');
}

/**
 * 主任指定独任制仲裁员：点击确定
 * @param type UI类型 //TODO 需要判断类型
 */
const appointSingleNext = async (type = 0) => {
    await page.click('.ant-col-16  button:nth-child(2)');//确认
    await page.waitFor(1000);
    let modalBtn = await page.$$('.ant-modal-footer div button')
    await modalBtn[1].click();

    await page.waitFor('.comeback');
    await page.click('.comeback');
    await ajax.waitForResponse(page, 'getAllCaseInfo.do');
}

/**
 * 主任指定合议制仲裁员
 */
const appointDiscussion = async () => {
    //判断是否是合议制
    if (tribunal.secretary.courtyardStyle !== 1) {
        return;
    }

    //TODO 1.选首席参考独任制；2.边裁:选0个，选1/2个
    await page.waitFor('.seachBox');
    const url = page.target()._targetInfo.url;
    //way的值：1为独任，2为边裁，3为首席，4为1首席2边裁
    if (url.indexOf('way=4') > -1) {
        await collegiateSystem();//选边裁与首席
        await appointSingleNext();
    }
}

/**
 * 秘书发送通知
 */
const sendNotice = async () => {
    await acceptSingle();
    await acceptDiscussion();
    await tribunalNotice()//登陆秘书账号发通知
    await fillOpenTime();
}

/**
 * 独任制受理
 */
const acceptSingle = async () => {
    if (tribunal.secretary.courtyardStyle !== 0) {
        return;
    }

    //独任制登陆秘书账号发通知
    await selectedArbitratorNotice();
    //独任接受选定
    if (tribunal.zhuren.chiefId[0].id) {
        console.log('独任制')
        await arbitratorlogin(tribunal.zhuren.chiefId[0].id);
    }
}

/**
 * 合议制受理
 */
const acceptDiscussion = async () => {
    if (tribunal.secretary.courtyardStyle !== 1) {
        return;
    }
    console.log('合议制')
    //合议制登陆秘书账号发通知
    await selectedArbitratorNotice2()

    //TODO 如果不需要主任指定，就需要获取当事人选定的首席
    //首席接受选定
    if (tribunal.zhuren.chiefId.id) {
        await arbitratorlogin(tribunal.zhuren.chiefId.id);
    }
    //边裁接受选定
    if (tribunal.zhuren.linesmanId.length > 0) {
        for (let item of tribunal.zhuren.linesmanId) {
            await arbitratorlogin(item.id)
        }
    }
}

/**
 * 保存案件信息
 */
const saveCaseInfo = async () => {
    const { zhuren, secretary } = tribunal;
    caseInfo.chiefArbitratorId = zhuren.chiefId.id;
    caseInfo.courtType = secretary.hearWays - 2;
}

/**
 * 主任选择边裁与首席-合议制
 */
const collegiateSystem = async () => {
    await ajax.waitForResponse(page, 'arbitratorRecommend.do');
    let chief = await page.$$('.selectingAibitratorWidget-query-list .ant-col-5 button span');
    await chief[0].click();
    await chief[2].click();
    await chief[5].click();
}

/**
 秘书签章生成仲裁员选定情况通知书-独任制
 */
const selectedArbitratorNotice = async () => {
    await login(pupConfig, caseInfo.secretaryId)//登陆秘书
    await caseDetail(pupConfig, caseInfo.caseId, 0);
    await signaturePreview(page); //签章
    await screen('仲裁员选定情况已签章')
    await page.waitFor('.addressee_submit');//确认按钮
    await page.click('.addressee button.addressee_submit');
    await page.waitFor(1000);//弹框再次确认
    await page.waitFor('.addresseeDialog');
    await page.click('.ant-modal-footer div button:nth-child(2)');
    await page.waitFor('.zc-detail');
    await screen('仲裁员选定情况通知书已发送')
}

/**
 秘书签章生成仲裁员选定情况通知书-合议制
 */
const selectedArbitratorNotice2 = async () => {
    await login(pupConfig, caseInfo.secretaryId)//登陆秘书
    await caseDetail(pupConfig, caseInfo.caseId, 0);
    await page.waitFor('.materialConfirm_table a')
    for (let i = 0; i < 3; i++) {
        await page.click('.materialConfirm_table a:nth-child(1)')
        await signaturePreview(page); //签章
        await page.waitFor('.materialConfirm_table a')
        await page.waitFor(2000)
    }
    await page.click('button.materialConfirm_submit')
    await ajax.waitForResponse(page, 'queryConfirmedArbitratorList.do');//确认收件人
    await page.click('button.ant-btn-primary');
    await page.waitFor(1000);
    await page.click('.ant-modal-footer button.ant-btn-primary');
}

//仲裁员接受
const arbitratorlogin = async (id) => {
    await login(pupConfig, id);//登陆仲裁员账号（独任制）
    await caseDetail(pupConfig, caseInfo.caseId, 1);

    await page.waitFor('.ant-checkbox-wrapper input');
    let box = await page.$$('.ant-checkbox-wrapper input');
    await box[4].click();
    await box[0].click();
    await box[1].click();
    await box[2].click();
    await page.click('.activeRefereeBook-footer-btn button.ant-btn')
    await signaturePreview(page)
}

//秘书发送组庭通知书
const tribunalNotice = async () => {
    await login(pupConfig, caseInfo.secretaryId);
    await caseDetail(pupConfig, caseInfo.caseId, 0);
    await signaturePreview(page)
    //等待页面加载
    await ajax.waitForResponse(page, 'materialConfirmation.do');
    //材料确认
    await page.click('.materialConfirm_submit_box button.ant-btn')
    //确认收件人
    await ajax.waitForResponse(page, 'queryCorrelateList.do');
    await screen(page, '秘书发送组庭通知书')
    await page.click('.confirmAddressee button.ant-btn');
    await page.waitFor(1000);//弹出框确认
    await page.click('.ant-modal-footer div button.ant-btn:nth-child(2)')
    await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');

}
//填写开庭时间和地点
const fillOpenTime = async () => {
    await caseDetail(pupConfig, caseInfo.caseId, 0);
    await courtTime(pupConfig)//填写开庭时间和地点
    await signaturePreview(page)//签章
    await page.waitFor('.confirmAddressee');//确认收件人
    await page.click('.confirmAddressee button.ant-btn')
    await page.waitFor(1000);//弹出框确认
    await page.click('.ant-modal-footer div button.ant-btn:nth-child(2)')
}

const screen = async (name = '案件关联') => {
    await page.screenshot({ path: config.imgPath + 'tribunal/' + name + '.png' });
}
module.exports = start;
